package com.example.takeaway.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.takeaway.common.R;
import com.example.takeaway.entity.*;
import com.example.takeaway.service.CategoryService;
import com.example.takeaway.service.DishService;
import com.example.takeaway.service.SetMealService;
import com.example.takeaway.service.SetmealDishService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */

@RestController
@RequestMapping("/setmeal")
@Slf4j
@AllArgsConstructor
public class SetmealController {


    private final SetmealDishService setmealDishService;
    private final SetMealService setMealService;
    private final CategoryService categoryService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */

    @RequestMapping
    public R<String> AddMeal(@RequestBody SetmealDto setmealDto){


          log.info("新增套餐信息：{}",setmealDto);
          setMealService.saveWithDish(setmealDto);


        return R.success("新增套餐成功");
    }


    /**
     * 查询套餐数据
     */

    @GetMapping("/page")
    public R<Page<SetmealDto>> GetMealList(Integer page , Integer pageSize , String name){


        //new个page对象
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();

        //查询条件
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null ,Setmeal::getName,name);
        wrapper.orderByDesc(Setmeal::getUpdateTime);

        //搜寻
        setMealService.page(setmealPage,wrapper);
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");


        List<SetmealDto> collect = setmealPage.getRecords().stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);

            //获取分类id
            Long categoryId = item.getCategoryId();

            Category byId = categoryService.getById(categoryId);
            if(byId != null){
                String categoryName = byId.getName();
                setmealDto.setCategoryName(categoryName);
            }


            return setmealDto;

        }).collect(Collectors.toList());


        setmealDtoPage.setRecords(collect);

        return R.success(setmealDtoPage) ;


    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */

    @DeleteMapping
    public R<String> delete(@RequestParam  List<Long> ids){

        log.info("ids:{}",ids);
        setMealService.removeWithDish(ids);

        return R.success("套餐数据成功删除");
    }


    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */

    @GetMapping("/list")
    public R<List<Setmeal>> GetList(Setmeal setmeal){


        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        wrapper.eq(Setmeal::getStatus , 1 );
        wrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setMealService.list(wrapper);


        return R.success(list);
    }





}
